<template>
  <el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
    <el-form-item prop="userInfo">
      <el-input v-model="form.userInfo" :placeholder="$t('login.userPlaceholder')" clearable
                prefix-icon="el-icon-user">
        <template #append>
          <el-select v-model="form.tenantName" style="width: 130px;">
            <el-option v-for="item in tenantNameList" :label="item.tenantName" :value="item.tenantName"></el-option>
          </el-select>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="form.password" :placeholder="$t('login.PWPlaceholder')" clearable
                prefix-icon="el-icon-lock"
                show-password></el-input>
    </el-form-item>
    <el-form-item style="margin-bottom: 10px;">
      <el-col :span="12">
        <el-checkbox v-model="form.autologin" :label="$t('login.rememberMe')"></el-checkbox>
      </el-col>
      <el-col :span="12" class="login-forgot" v-if="showReset === 'Y'">
        <router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
      </el-col>
    </el-form-item>
    <el-form-item>
      <el-button :loading="islogin" round style="width: 100%;" type="primary" @click="login">
        {{ $t('login.signIn') }}
      </el-button>
    </el-form-item>
    <div class="login-reg" v-if="showRegister === 'Y'">
      {{ $t('login.noAccount') }}
      <router-link to="/user_register">{{ $t('login.createAccount') }}</router-link>
    </div>
  </el-form>
</template>

<script>
module.exports = {
  props: {
    showRegister: {type: String, default: "N"},
    showReset: {type: String, default: "N"},
    showHour: {type: String, default: "N"},
  },
  data() {
    return {
      form: {
        tenantName: null,
        userInfo: "demo",
        password: "demo",
        autologin: this.showHour === 'Y'
      },
      rules: {
        tenantName: [
          {required: true, message: this.$t('login.tenantError'), trigger: 'blur'}
        ],
        userInfo: [
          {required: true, message: this.$t('login.userError'), trigger: 'blur'}
        ],
        password: [
          {required: true, message: this.$t('login.PWError'), trigger: 'blur'}
        ]
      },
      islogin: false,
      tenantNameList: []
    }
  },
  created() {
    this.$API.tenant.listTenantName.get().then(response => {
      if (response.status) {
        this.tenantNameList = response.data
        if (this.tenantNameList.length !== 0) {
          this.form.tenantName = this.tenantNameList[0].tenantName
        }
      }
    })
  },
  methods: {
    login() {
      if (!this.form.tenantName) {
        this.$message.warning("请选择租户")
        return
      }
      this.$refs["loginForm"].validate(async valid => {
        if (valid) {
          this.islogin = true
          let data = {
            tenantName: this.form.tenantName,
            userInfo: this.form.userInfo,
            password: this.$TOOL.crypto.rsaEncrypt(this.form.password)
          }
          // 获取token
          this.$API.auth.login.post(data).then(response => {
            if (response.status) {
              this.$TOOL.data.set("APP_TOKEN", response.data.token, this.form.autologin ? 24 * 60 * 60 : 60 * 60 * 24 * 7)
              this.$TOOL.data.set("APP_USER_INFO", response.data.userInfo, this.form.autologin ? 24 * 60 * 60 : 60 * 60 * 24 * 7)
              this.$TOOL.data.set("APP_TENANT_INFO", response.data.tenantInfo, this.form.autologin ? 24 * 60 * 60 : 60 * 60 * 24 * 7)
              this.$TOOL.data.set("APP_USER_PERMISSIONS", response.data.userInfo.permissions, this.form.autologin ? 24 * 60 * 60 : 60 * 60 * 24 * 7)
              this.$API.menu.listMenuByUser.get(response.data.userInfo.userId).then(res => {
                if (res.status) {
                  this.$TOOL.data.set("APP_MENU_INFO", res.data)
                  this.$router.replace({
                    path: '/'
                  })
                  this.$message.success(response.data.userInfo.nickName + " 欢迎您登录 " + this.$CONFIG.APP_NAME)
                } else {
                  this.$alert(response.msg, "提示", {type: 'error'});
                  this.islogin = false
                  return false
                }
              })
            } else {
              this.$alert(response.msg, "提示", {type: 'error'});
              this.islogin = false
              return false
            }
          })
        }
      });
      this.islogin = false
    },
  }
}
</script>

<style>
</style>
